﻿using System;
using System.Collections.Generic;

namespace OrderControl {
    class Order {
        private int id;
        private List<OrderDetail> details;
        private Customer customer;
        private DateTime time;
        private string shippingAddress;

        public int Id {
            get => id;
            set {
                if (value > 0) {
                    id = value;
                }
                else {
                    throw new OrderPropertyException("Order id cannot be less than or equal to zero.");
                }
            }
        }
        public List<OrderDetail> Details { get => details; }
        public Customer Customer { get => customer; }
        public DateTime Time { get => time; set => time = value; }
        public double TotalPrice {
            get {
                double totalPrice = 0;
                foreach (OrderDetail orderDetail in Details) {
                    totalPrice += orderDetail.Price;
                }
                return totalPrice;
            }
        }
        public string ShippingAddress {
            get => shippingAddress;
            set {
                if (value != null && value.Length > 0) {
                    shippingAddress = value;
                }
                else {
                    throw new OrderPropertyException("Shipping address cannot be empty.");
                }
            }
        }

        public Order(int id, Customer customer, DateTime time, String shippingAddress) {
            this.Id = id;
            details = new List<OrderDetail>();
            this.customer = customer;
            this.Time = time;
            this.ShippingAddress = shippingAddress;
        }

        public override bool Equals(object obj) {
            return obj is Order order && id == order.id;
        }
        public override string ToString() {
            return "[Order] Id: " + Id
                + ", Customer: " + customer.Name
                + ", Time: " + Time
                + ", Total Price: " + TotalPrice
                + ", Shipping address: " + ShippingAddress;
        }
    }
}
